package com.peng.leetcode.palindrome;

/**
 * CountSubstrings
 * <p>
 * 647. 回文子串
 * <p>
 * 使用中心扩展法查询搜索,
 * 中心计算， 先将总长度 * 2 - 1
 * 左指针 = i / 2;
 * 有指针 = i / 2 + i % 2;
 * <p>
 * 左右指针向两边滑动
 *
 * @author: lupeng6
 * @create: 2021/4/7 14:40
 */
public class CountSubstrings {

    public static void main(String[] args) {
        String s = "abc";
        System.out.println(new CountSubstrings().countSubstrings(s));
    }

    public int countSubstrings(String s) {
        int length = s.length();
        int answer = 0;

        for (int i = 0; i < length * 2 - 1; i++) {
            int l = i / 2;
            int r = i / 2 + i % 2;
            while (l >= 0 && r < length && s.charAt(l) == s.charAt(r)) {
                l--;
                r++;
                answer++;
            }
        }
        return answer;
    }


}
